Skip to content

DXF import: a SPLINE keeps its knots and weights, within 0.001 mm proven (#9355) - #9356

Closed
rmorgans wants to merge 7 commits into
tangency-labs:mainfrom
grumpydevorg:dxf-spline-knots
Closed

rmorgans wants to merge 7 commits into
tangency-labs:mainfrom
grumpydevorg:dxf-spline-knots

Conversation

@rmorgans

@rmorgans rmorgans commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Change

Fixes #9355. Builds on #10047 (#10046), which must merge first; until then this branch carries its commit.

A SPLINE imported from DXF comes in as the curve its file describes, using its knots (40) and weights (41), and within 0.001 mm of it (or the fit_tolerance given), with that bound proven rather than sampled.

  • Core (crates/geom/src/bspline/curve.rs): follow(piece, tolerance, max_degree, max_fits) gives the curves a sketch can hold for one Bézier piece of a NurbsCurve, each a Certified<Fit> with a proven bound on its distance from the piece:

    • the piece itself, when it is a polynomial Bézier up to how its parameter runs (weights 1, c, c², …) and of degree 9 or less;
    • a conic, for a rational quadratic, only where the sketch holds a conic at that very weight: 1 − rho at least MIN_CONIC_RHO_GAP (10⁻⁹), the constant the sketch's conic code now uses too;
    • otherwise cubics that match the ends of parts of the piece and their tangents, each part halved until it is within the tolerance;
    • no fit has neighbouring control points nearer than 10⁻⁹ mm in the sketch (MIN_CONTROL_GAP, the sketch spline tool's own rule), whatever the insert's scale: the gap is divided by the scale like the tolerance. A spline whose points all lie within that gap is a point, not a curve: it is left out, and the reply's warnings counts it. At an end with no direction (a repeated point, as a zero-length handle leaves) the curve arrives with no speed and turns ever more sharply into the end. The cubic there is compared with the part as traced by a parameter that stops at that end too (ψ = s², 1 − (1 − s)² or 3s² − 2s³, composed exactly in intervals), so its handle can be long and point the way the curve leaves the end: it bends gently where the file's curve bends sharply, within its proven bound.

    The bound covers (C·W − N·V)/(V·W) in the Bernstein basis, computed in the outward-rounded intervals that already hold the piece's exact control points, so it holds for the exact curve. A fit is refused, with how close it came, when it would take more than max_fits curves or the intervals can't prove it.

  • Reader (crates/io/src/dxf.rs: the SPLINE arm, and Affine::apply, which the INSERT expansion now calls with ?):

    • A SPLINE with knots (40) or weights (41) reads as Geom2::Nurbs(NurbsCurve), exactly as written. The type can only hold a valid curve. Blocks move it by its control points.
    • Refused: knots or weights that don't describe a curve (wrong count, decreasing, a weight that isn't positive, weights without knots), or a degree above 16, refuse the drawing and say why. Before, they silently fell back to even knots.
    • A SPLINE without knots or weights reads as before.
  • Insert DXF (crates/engine/src/cmd/sketch_import.rs):

    • The sketch's own spline: a B-spline whose knots are clamped and evenly spaced up to a number's rounding (4ε of the domain), with no weights or all weights equal, degree 9 or less and at most 500 points, comes in as one editable spline, as before. Knots written to fewer digits (say 0.333333) describe a different curve, which comes in as its exact pieces.
    • Tolerance follows the scale, and leaves room for joins: fit_tolerance (mm, 0.0001 to 10, default 0.001; separate from tolerance, the distance within which ends join) sets how close a spline comes in. Each other B-spline is split after the insert's scale is known, within half of it in the sketch (0.0005 mm by default, divided by the scale). The other half is for joining the curves' ends to other curves. When a join tolerance above that moves an end further, the reply's warnings says how far, since the spline can then be that far from the file's curve at that end.
    • Mapping to sketch curves: polynomial pieces become control-point splines (lines at degree 1) and rational quadratics become conics. Other rational pieces become cubic control-point splines, and the reply's warnings gives the largest proven distance.
    • Over 500 points: a control-point spline over the sketch's 500-point limit is split the same way; before, the points past the 500th were dropped.
    • Loosened, not refused, when it can't be followed that close: a piece that can't be followed within the fit tolerance (in up to 4096 curves) comes in within the first tolerance 4, 16, … times looser (up to 4³²) that it can be, and the reply's warnings gives the proven distance it was followed within (an end joined to another curve can move further, up to the join tolerance). That is a bounded fallback with a proven bound, not the closest fit there is. Still refused, with the reason: a spline that isn't a curve, a piece whose intervals can't prove any fit (its points don't project, its weights lose their precision) or that still can't be followed after the last loosening, and a drawing whose splines make more than 200 000 curves.
  • Repeated control points (Insert DXF and Insert SVG): a spline that repeats a control point is never the sketch's own spline; it comes in as cubics that don't, within the fit tolerance, and the reply's warnings says so. An SVG outline with a zero-length handle (C4,31 8,20 8,20) now extrudes, and an offset wall around it too. main drops the repeated point, which changes that curve by up to 0.098 mm, silently.

  • Canvas to Sketch (canvas.to_sketch): its traced cubics go through the same check, so a vertex with no tangent no longer gives a sketch spline with a repeated point.

  • Joining ends: only a spline's ends, and a conic's ends, join nearby points now. Before, every control point (and every fit point of a through-point spline) and a conic's apex snapped to any point within the join tolerance (up to 10 mm), which moved the curve. This also applies to Insert SVG.

  • Very long control SPLINEs: one with no knots and more than 16 380 control points is refused with the reason; before, it was silently cut to its first 500.

What "within 0.001 mm" leaves out: the rounding of the sketch's coordinates (about 10⁻¹⁶ of their size), and an end joined further than 0.0005 mm, which is reported.

Not here: the DXF closed flag (70) is not read; a closed spline comes in closed through its own knots and points. The sketch's own spline code (bspline_at, bspline_beziers) is unchanged. No design-format change.

Changes a default: no

Validation

Rebased onto main at a5b71ca20. On this head, macOS arm64, cargo xtask ci on the tree committed: every check before the tests passes (fmt, assets, icons, licences, lists, skills, layers, ui-containers, mesh-decisions, clippy -D warnings, the wasm32 check). The tests fail in 40 places: 39 fail on main at a5b71ca20 too, run the same way, and the 40th is the wall-clock a_long_helical_tangent_chain_is_found_quickly, which failed only under the full run's load and passes run alone.

Tests check against independent evaluations, not SolidCraft's own output:

  • geom::bspline (double-double, about 106-bit, de Boor as the reference; the bound checked both ways, as the distance from every sampled point of one to the other):
    • the piece itself, including with weights 1, 2, 4, 8 and a weighted line;
    • conics for weighted quadratics with end weights other than 1; not for collinear ones, nor for a middle weight of 2·10⁹, whose conic the sketch would hold at another weight (fails with the MIN_CONIC_RHO_GAP check removed);
    • a weighted cubic at three tolerances;
    • limits (no room, a tolerance below rounding, a 100 m curve with weights 10⁸ apart);
    • a cubic with no direction at its end (the SVG's), at both ends, and a rational one, at 0.0005 mm and 10⁻⁶ mm, each handle shortened towards none: no fit repeats a point, the cubic leaves a stalled end the way the curve does; the SVG's curve comes into a sketch as 5 cubics at the default tolerance;
    • a curve 10⁻²⁰⁰ across, and a piece over two adjacent numbers near 10⁹;
    • a fuzz run over 3000 random curves, valid and not.
  • io a_spline_keeps_its_own_knots_and_weights: even or uneven knots, unclamped, degree 12, weights, a block turned a quarter, and the refusals; the drawing writer's round trip now reads its SPLINEs back as the B-splines written.
  • engine, end to end through sketch.insert_dxf (f64 de Boor as the reference):
    • own knots, a double knot, a closed cubic that closes its profile;
    • repeated control points ([A, A, B, C] as the sketch's own spline would take it, [A, B, C, C], an exact piece starting so, an SVG path's zero-length handle, a 3·10⁻⁸ handle inserted at scale 0.01, split and unsplit): no two neighbouring points in the sketch within 10⁻⁹ mm, within 0.001 mm, with the warning;
    • a spline smaller than the gap, at scale 1 and the same shape 100× larger at scale 0.01, is left out and counted; one just larger comes in;
    • fit_tolerance 0.1, 0.001 and 0.0001 mm: within each, fewer curves the looser it is; out of its bounds refused;
    • a 1000 km curve with weights 10⁸ apart that can't be followed within 0.001 mm in 4096 curves comes in, within the distance its warning gives;
    • 520 points with uneven, even and no knots;
    • conics on their circle;
    • a weighted cubic within 0.001 mm at scale 1 and 40, with its warning;
    • an inner control point that joins no other curve;
    • the sketch's own spline (even knots, no weights or weights alike) comes in as one editable spline; knots written to six digits come in as exact pieces within 0.001 mm;
    • a quadratic with middle weight 2·10⁹ comes in as cubics within 0.001 mm, not as a conic (fails with the MIN_CONIC_RHO_GAP check removed);
    • a spline end joined to a line 0.01 mm away with a join tolerance of 0.05 mm is reported, both for a weighted cubic split into pieces and for the sketch's own spline; with the default tolerance it doesn't join and keeps within 0.001 mm (fails with the report removed);
    • 150 random SPLINEs of degree 1 to 11 at three scales, each within 0.001 mm both ways or refused with the reason;
    • freeform_offsets_trim_folds_and_round_chain_corners_into_an_extrudable_wall (sketch offsets): its SVG outline with a zero-length handle now comes in as more curves than it is written with, and its offset walls, outward and inward, extrude. Kept exactly, the curve's wall failed SMLib's check (Bad Loop Orient), and its outline alone CurveCusp.

Screenshots of the behavior

One DXF ($INSUNITS mm), inserted with solidcraft-cli before (main) and after. Measured before the rebase onto the SMLib kernel; the code they measure is unchanged since, except for repeated control points, which these three SPLINEs don't have. It holds three SPLINEs: a 60-point horn wall with knot gaps varying 2.95×, a weighted quarter circle of r 25, and a weighted cubic.

The table gives the largest distance between the file's curve (by de Boor) and the result:

  • "exported lines" measures both builds the same way, through sketch.export_dxf, and includes the export's own chord error;
  • "sketch curves" measures the after build both ways, against the curves the saved design holds, evaluated from their definition.
SPLINE before, exported lines after, exported lines after, sketch curves after, the import's proven bound
horn wall, uneven knots 8.2e-3 mm 1.2e-4 mm 5.7e-14 mm exact pieces
weighted quarter circle 1.52 mm 3.7e-3 mm 5.6e-14 mm exact (a conic)
weighted cubic 6.70 mm 2.1e-4 mm 2.3e-4 mm 4.5e-4 mm (33 cubics, with a warning)

Before (the weighted cubic is plainly the wrong shape; the other two are off by less than the picture shows):

before

After:

after

Integration status

Rebuilt on #10047 (NurbsCurve, exact Bézier pieces in intervals), itself rebased onto main at a5b71ca20; main's MAX_SPLINE_DEGREE (16, now from solidcore_record) is the one degree bound. Kernel pause: this changes crates/geom (B-spline curves for import), nothing in crates/kernel/. The earlier version of this PR fitted rational pieces by sampling; it is replaced, not kept. The Geom2::bspline helper agreed with @0xlillith on #9355 is gone: the reader keeps the file's curve as a NurbsCurve, and #8515 can build one from its DxfShape::Spline fields with NurbsCurve::from_homogeneous (explained on #9355).

rmorgans added a commit to grumpydevorg/solidcraft that referenced this pull request Oct 31, 2026
@rmorgans rmorgans changed the title DXF import: a SPLINE keeps its knots, weights and closed flag (#9355) DXF import: a SPLINE keeps its knots and weights, within 0.001 mm proven (#9355) Oct 31, 2026
… in one pass; knot insertion keeps a weight's precision (tangency-labs#10046)

- insert_knot takes both factors from knot differences: 1 − α near 1 lost a
  heavy weight's precision (8.3e-4 mm off on a 30 mm curve beside a weight of 1e12).
- NurbsCurve (private fields, from_homogeneous checks degree, knot count and order,
  domain, finite points within 1e12 mm, positive weights) and pieces(): one pass
  (NURBS Book A5.6), linear in length, rational or not, clamped or not.
- Clamping and the split run in outward-rounded intervals, so each piece holds the
  exact curve as given; one implementation of insertion and clamping serves f64
  and interval points (Blend).
- The hidden-line views use pieces() in place of their own quadratic split.

Tests against an independent double-double de Boor evaluation, including a fuzz
over 3000 random curves, valid and not.
…o room to clamp and a weight too small to divide by when made; the heavy-weight insertion tested (tangency-labs#10046)

Review of tangency-labs#10047:
- `insert_knot`'s factors from knot differences had no test of their own: the heavy-weight
  case used a curve already clamped. `boehm_insertion_keeps_the_curve` now inserts beside a
  weight of 10^12, which `1 - α` puts 8.3e-4 mm off.
- `from_homogeneous` refuses an inner knot repeated more than the degree (before, only
  `pieces()` did), knots too many to clamp (`pieces()` refused those as "too many knots"),
  and a subnormal weight, which was reported as a point that isn't finite.
- The random test checks every piece, not the first two.
…ven (tangency-labs#9355)

- geom: follow(piece, tolerance, max_degree, max_fits) gives the curves a
  sketch holds for a Bézier piece, each Certified with a bound proven in
  intervals on (C·W − N·V)/(V·W): the piece itself when it is a polynomial
  Bézier up to its parametrisation, a conic for a rational quadratic the
  sketch holds at that weight (1 − rho ≥ MIN_CONIC_RHO_GAP, now the sketch's
  own clamp too), else Hermite cubics halved until within the tolerance.
- io: a SPLINE with knots (40) or weights (41) reads as the curve written,
  Geom2::Nurbs(NurbsCurve); knots or weights that don't describe a curve
  refuse the drawing with the reason instead of falling back to even knots.
  Blocks move a NurbsCurve by its control points. Geom2::bspline is gone.
- engine: Insert DXF brings in the sketch's own spline (even clamped knots,
  weights alike) as one editable spline, any other within 0.0005 mm in the
  sketch, leaving the other half of 0.001 mm for joining ends; an end moved
  further by a larger join tolerance is reported, as is how close fitted
  pieces are. Only a spline's and a conic's ends join nearby points; inner
  control points and a conic's apex no longer snap.
…rection; fit_tolerance, loosened when a spline can't be followed that close (tangency-labs#9355)

A control point repeated (a zero-length handle, as drawing programs write `C4,31 8,20 8,20`)
leaves a spline's end with no direction: the sketch's own spline tool refuses such points and the
solid kernel won't build on them (SmLoop "Bad Loop Orient", BSplineCurve "CurveCusp"). Such a
spline now comes in as cubics that repeat no point, each within the fit tolerance with the bound
proven: at an end with no direction the cubic is compared as traced by a parameter that stops
there too, so its handle can be long and the cubic bends gently where the file's curve turns ever
more sharply. The import warns that it did.

`fit_tolerance` (mm, 0.0001 to 10, default 0.001) sets how close a spline comes in, apart from
the join `tolerance`. A piece that can't be followed that close in 4096 curves comes in within the
first tolerance 4, 16, ... times looser (at most 32 times) that it can be, with a warning giving
the proven distance. Still refused: a spline that isn't a curve, a piece no loosening lets be
followed or whose intervals can't prove a fit, and a drawing whose splines make more than
200 000 curves.
… in the sketch at any scale; a spline smaller than that is left out and counted (tangency-labs#9355)

Review findings fixed:
- The no-repeated-point gap was checked in drawing units before the insert's
  scale: follow, cubics, hermite and repeats take the gap, and the importer
  passes MIN_CONTROL_GAP / k, also to its own repeats and sketch_own, so a
  scaled-down handle (split or unsplit) is followed by curves with a direction.
- A spline whose control points all lie within the gap came in as a closed
  micro-spline with a misleading warning: it is left out and the warnings
  count it; a fit of a piece that small is left out too.
- Warnings no longer give a cause that wasn't established (4096 curves), say
  "followed within" and that a joined end can move up to the join tolerance;
  follow_loosened tries at most MAX_LOOSENINGS loosenings, as documented.
- Canvas to Sketch passes its traced cubics through split_nurbs, so a
  zero-length handle there no longer reaches add_geometry, as its comment says.
- A test comment counted four SVG curves where the path has five.
@coderabbitai

coderabbitai Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

@bherbruck

Copy link
Copy Markdown
Collaborator

Thanks for this! I'm closing older PRs that have gone quiet or drifted from main, since a lot has changed in the last couple of weeks. If you'd still like this in, please open a fresh PR against current main; happy to review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DXF import: a SPLINE's knots, weights and closed flag are ignored, so its shape changes

2 participants